package com.vcredit.creditcat.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vcredit.bird.enumtype.MobileCodeEnum;
import com.vcredit.creditcat.model.AuthMobile;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustSupplementItem;
import com.vcredit.creditcat.model.ProcessSpecialItem;
import com.vcredit.creditcat.persistence.AuthMobileExtMapper;
import com.vcredit.creditcat.persistence.base.AuthMobileMapper;
import com.vcredit.creditcat.service.ext.ProcessSpecialItemExtService;
import com.vcredit.creditcat.vo.auth.AuthMobileExt;

/**
 * 
 **/
@Service
public class AuthMobileService {
	@Autowired
	private AuthMobileMapper mapper;
	@Autowired
	private AuthMobileExtMapper extMapper;
	@Autowired
	private CustSupplementItemService custSupplementItemService;
	@Autowired
	private ProcessSpecialItemExtService processSpecialItemExtService;

	public void insert(AuthMobile obj) {
		mapper.insert(obj);
	}

	public void update(AuthMobile obj) {
		mapper.update(obj);
	}

	public List<AuthMobile> findAll() {
		return mapper.findAll();
	}

	private List<AuthMobile> find(AuthMobile obj) {
		return mapper.find(obj);
	}

	public List<AuthMobile> findDistinctMobile(AuthMobile obj) {
		return mapper.findDistinctMobile(obj);
	}

	public AuthMobile findByProcessIdAndType(Long processId, int platformType) {
		if (null == processId) {
			return null;
		}
		AuthMobile param = new AuthMobile();
		param.setProcessId(processId);
		param.setPlatformType(platformType);
		List<AuthMobile> list = find(param);
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	public List<AuthMobile> findByCust(Long processId) {
		AuthMobile param = new AuthMobile();
		param.setProcessId(processId);
		return this.find(param);
	}

	public List<AuthMobile> findUnCrawl() {
		return extMapper.findUnCrawl();
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateProcess(AuthMobile param) {
		AuthMobile authMobile = findByProcessIdAndType(param.getProcessId(), param.getPlatformType());
		if (null != authMobile) {
			param.setMobileProcessId(authMobile.getMobileProcessId());
			if (param.getMobileStatus().equals(MobileCodeEnum.INIT.getCode())) { // 清空原有记录
				param.setRegisterName("");
				param.setRegisterIdNo("");
				param.setMobileScore("");
				param.setExpiredTime("");
			}
			update(param);
		} else {
			insert(param);
		}

	}

	public void resetStatus(AuthMobile param) {
		update(param);
	}

	public AuthMobileExt findExtById(long id) {
		AuthMobile mobile = mapper.findById(id);
		AuthMobileExt mobileExt = new AuthMobileExt();
		if (null != mobile) {
			BeanUtils.copyProperties(mobile, mobileExt);
		}
		return mobileExt;
	}

	/**
	 * 根据用户和产品查询对应的手机认证信息
	 * 
	 * @author zhaoqi01
	 * @param cust
	 * @param productId
	 * @return
	 */
	public AuthMobile findByCustAndProduct(Cust cust, Long productId) {
		ProcessSpecialItem processSpecialItem = processSpecialItemExtService.findByProductForMobileItem(productId);
		if (null == processSpecialItem) {
			return null;
		}

		if (processSpecialItem.getItemType() == 0) { // 原始认证项
			return this.findByProcessIdAndType(cust.getLastProccessId(), 0);
		} else if (processSpecialItem.getItemType() == 1) { // 补充认证项
			CustSupplementItem custSupplementItem = custSupplementItemService.findByCustAndSupplementItemId(cust.getCustId(), processSpecialItem.getItemId());
			if (null == custSupplementItem || custSupplementItem.getDataEntity() == null) {
				return null;
			}
			return mapper.findById(Long.parseLong(custSupplementItem.getDataEntity()));
		}
		return null;
	}
}